package com.example.controller.admin;

import com.example.dto.BookDishDTO;
import com.example.result.PageBeanResult;
import com.example.result.Result;
import com.example.service.BookDishService;
import com.example.vo.BookDishVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Scanner;
import java.util.Set;

@RestController
@Slf4j
@Tag(name = "图书菜单")
@RequestMapping("/admin/dish")
public class BookDishController {

    @Autowired
    private BookDishService bookDishService;

    @Autowired
    private RedisTemplate redisTemplate;



    /**
     * 分页展示所有图书
     */
    @GetMapping("/page")
    @Operation(summary = "分页展示所有图书")
    public Result showBook(@RequestParam (required = false)String name,
                           @RequestParam (required = false)Integer categoryId,
                           @RequestParam (required = false)Integer status,
                           @RequestParam(defaultValue = "0") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页展示所有图书:{}",page,pageSize,name,categoryId,status);
        PageBeanResult pageBeanResult=bookDishService.showBook(name,categoryId,status,page,pageSize);
        return Result.success(pageBeanResult);
    }

    /**
     * 根据名字查找图书
     */
    @GetMapping("/pages")
    @Operation(summary = "根据名字查找图书")
    public Result selectBook(String name,
                             @RequestParam(defaultValue = "0") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("按名字查询图书，{},{},{}",name,page,pageSize);
        PageBeanResult pageBeanResult=bookDishService.selectBook(name,page,pageSize);
        return Result.success(pageBeanResult);

    }
    /**
     * 删除图书
     */
    @DeleteMapping("delete")
    @Operation(summary = "删除图书")
    public Result deleteBook(Integer id){
        log.info("根据ID删除图书:{}",id);
        bookDishService.deleteBook(id);
        return Result.success();

    }
    /**
     * 批量删除图书
     */
    @DeleteMapping
    @Operation(summary = "批量删除图书")
    @CacheEvict(cacheNames = "BookCache" ,allEntries = true)
    public Result deleteBooks(@RequestParam List<String> ids){
        log.info("批量删除图书:{}",ids);
        bookDishService.deleteBooks(ids);

        //将所有缓存的数据清理
        //cleanCache("dish_*");
        return  Result.success();
    }

    /**
     * 是否售完 1为未售完 0为售完
     */
    @PostMapping("/status/{status}")
    @Operation(summary = "销售状态")
    @CacheEvict(cacheNames = "BookCache" ,allEntries = true)
    public Result starOrStop(@PathVariable Integer status,Integer id){
        log.info("图书的销售状态，根据id更改:{},{}",status,id);
        bookDishService.starOrStop(status,id);

        //将所有缓存的数据清理
        //cleanCache("dish_*");
        return Result.success();
    }
    /**
     * 数据回显
     */
    @GetMapping("/{id}")
    @Operation(summary = "数据回显")
    public Result getById(@PathVariable Long id){
        log.info("数据回显:{}",id);
        BookDishVO bookDishVO =bookDishService.getById(id);
        return Result.success(bookDishVO);
    }
    /**
     * 修改图书信息
     */
    @PutMapping
    @Operation(summary = "修改图书信息")
    @CacheEvict(cacheNames = "BookCache" ,allEntries = true)
    public Result updateBook(@RequestBody BookDishDTO bookDishDTO){
        log.info("修改图书信息:{}",bookDishDTO);
        bookDishService.updateBook(bookDishDTO);


        //将所有缓存的数据清理
        //cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 新增图书
     */
    @PostMapping
    @Operation(summary = "新增图书")
    @Cacheable(value= "BookCache",key = "#bookDishDTO.categoryId")
    public Result addBook(@RequestBody BookDishDTO bookDishDTO) {
        log.info("新增图书,{}",bookDishDTO);
        bookDishService.addBook(bookDishDTO);

        //清理缓存数据
        //String key ="dish_"+bookDishDTO.getCategoryId();
        //cleanCache(key);
        return Result.success();


    }

    /**
     * 根据分类id查询图书
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "根据分类id查询图书")
    public Result list(Long categoryId) {
        List<BookDishDTO> list = bookDishService.list(categoryId);
        return Result.success(list);
    }


    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);

    }

}
